//面试题 01.02. 判定是否互为字符重排
class Solution {
public:
    bool CheckPermutation(string s1, string s2) {
        int n1 = s1.size();
        int n2 = s2.size();
        if (n1 != n2)
            return false;
        int hash[26] = {0};
        for (int i = 0; i < n1; i++) {
            hash[s1[i] - 'a']++;
        }
        for (int i = 0; i < n1; i++) {
            hash[s2[i] - 'a']--;
            if (hash[s2[i] - 'a'] < 0)
                return false;
        }
        return true;
    }
};


//存在重复元素
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> hash;
        for (auto& num : nums) {
            if (hash.count(num))
                return true;
            hash.insert(num);
        }
        return false;
    }
};

//存在重复元素 II
class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> hash;
        for (int i = 0; i < nums.size(); i++) {
            if (hash.count(nums[i]) && abs(hash[nums[i]] - i) <= k) {
                return true;
            }
            hash[nums[i]] = i;
        }
        return false;
    }
};